package com.rzico.sbl.ui.manager

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.TaxData
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class TaxActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<ManagerViewModel>()

    override fun initData() {
        initTitle(title = intent.getExtra("title"))

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关发票信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<TaxData>(R.layout.item_tax_list) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_tax_name, bean.name)
                            text(R.id.item_tax_money, bean.amount.formatDecimal())
                            text(R.id.item_tax_time, bean.createDate.getTimeString())

                            clicked {
                                startActivityEx<TaxDetailActivity>(
                                    "title" to intent.getExtra("title"),
                                    "data" to bean
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    /**
     * 发票列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .taxList(
                page = index,
                status = intent.getExtra("status"),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)

                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}